package cn.wolfcode.web.controller;

import cn.wolfcode.web.annotation.RequiredPermission;
import cn.wolfcode.web.domain.Employee;
import cn.wolfcode.web.jsonResult.JsonResult;
import cn.wolfcode.web.mapper.EmployeeMapper;
import cn.wolfcode.web.service.IResetPasswordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;

@Controller
public class ResetPasswordController {
    @Autowired
    private EmployeeMapper employeeMapper;
    @Autowired
    private IResetPasswordService resetPasswordService;
    @Autowired
    private HttpSession session;

    @RequestMapping("/resetePassword")
    public String resetPassword(Long id,Model model) {
        Employee employee = resetPasswordService.resetePassword(id);
        model.addAttribute("employee", employee);
        session.setAttribute("id",id);
        return "/reset/resetPwd";//WEB-INF/views/reset/resetPwd.jsp
    }

    @RequestMapping("/reset/resetePassword")
    @ResponseBody
//    @RequiredPermission(value ="重置密码",expression ="resetPassword:selectPassword")
    public JsonResult selectPassword(String newPassword) {


        Long id = (Long) session.getAttribute("id");
        Employee employee = resetPasswordService.resetePassword(id);
        resetPasswordService.reset(newPassword, employee);
        employee.setPassword(newPassword);
        resetPasswordService.reset(newPassword, employee);
        return new JsonResult();
    }
}
